import os
import sys
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
import datetime
class LikeShopTest():
    def __init__(self) -> None:

        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)

    def test_shop_list(self):
        '''
        商品列表测试
        '''
        #点击商品按钮
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        sleep(2)
        shop_iframe = self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(shop_iframe)
        
        #定位商品名称并输入数据
        self.browser.find_element(By.ID,'keyword').send_keys('文具')
        #定位查询按钮并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/button[1]').click()
        sleep(1)
        #获取商品列表
        shop_list =  self.browser.find_elements(By.XPATH,' /html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
       
        if len(shop_list) == 0:
            print('没有数据')
        else:
            is_not_bug = True
            for item in shop_list:
               shop_name = item.find_element(By.XPATH,'./td[2]/div').text
               if '文具' not in shop_name:
                    is_not_bug = False
            if is_not_bug:
                print('没有问题')
            else:
                print('有bug')
                
    def test_login(self):
        '''
        登陆测试
        '''
        self.browser.get("http://likeshop.abc:8080/admin/account/login.html")
        sleep(2)
        self.browser.find_element(By.NAME,'account').send_keys('admin')
        
        self.browser.find_element(By.NAME,'password').send_keys('12345')
        
        self.browser.find_element(By.NAME,'code').send_keys('1111')
        
        self.browser.find_element(By.ID,'login').click()
        
        sleep(2)
        
        
    def test_shop_add(self):
        # 回到主框架
        self.browser.switch_to.default_content()
        #点击商品按钮
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        sleep(2)
        #定位子框架
        iframe1 = self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)
        
        # 点击发布商品
        self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
        sleep(3)
        #进入商品发布子框架
        shop_add_iframe = self.browser.find_element(By.XPATH,'/html/body/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(shop_add_iframe)
        #定位商品名称并输入数据
        now = datetime.datetime.now().strftime('%d/%m/%Y, %H:%M:%S')
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').send_keys('测试商品' + now)
        
        #定位第一个大类的下拉框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(1)
        # 定义第一个大类的下拉数据并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
        sleep(1)
        # 定义第二个大类的下拉框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(1)
        # 定义第二个大类的下拉数据并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[2]').click()
         # 定义第三个大类的下拉框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(1)
         # 定义第三个大类的下拉数据并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
        sleep(3)
        
        #定位商品主图 图片上传按钮
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        sleep(3)
        
        #进入图片上传子框架
        upload_img_iframe_1 = self.browser.find_element(By.XPATH,'/html/body/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(upload_img_iframe_1)
        self.test_upload_img('C:\\Users\\muzi\\Desktop\\软件测试\\1.png')
        
        # 回到主框架
        self.browser.switch_to.default_content()
        # 再进入商品子框架
        self.browser.switch_to.frame(iframe1)
        # 再进入商品发布子框架
        self.browser.switch_to.frame(shop_add_iframe)
        
        # 定位商品轮播图 图片上传按钮
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[6]/div/div/a').click()
        # 进入图片上传子框架
        sleep(3) 
        upload_img_iframe_2 = self.browser.find_element(By.XPATH,'/html/body/div[4]/div[2]/iframe')
        self.browser.switch_to.frame(upload_img_iframe_2)
        self.test_upload_img('C:\\Users\\muzi\\Desktop\\软件测试\\1.png')
        
        # 回到主框架
        self.browser.switch_to.default_content()
        # 再进入商品子框架
        self.browser.switch_to.frame(iframe1)
        # 再进入商品发布子框架
        self.browser.switch_to.frame(shop_add_iframe)
 
        # 点击价格库存
        self.browser.find_element(By.XPATH,'/html/body/div/ul/li[2]').click()
        sleep(2)
        
        # 定位价格、成本价、库存输入框并输入数据
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').send_keys(2)
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').send_keys(2)
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').send_keys(1)
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').send_keys(100)
        
        # 回到商品子框架进行保存操作
        # 回到主框架
        self.browser.switch_to.default_content()
        # 再进入商品子框架
        self.browser.switch_to.frame(iframe1)
        
        self.browser.find_element(By.LINK_TEXT,'保存').click()
        
    def test_upload_img(self,file_path):
         # 上传图片
        self.browser.find_element(By.XPATH,"//input[@type='file']").send_keys(file_path)
        sleep(5)
        self.browser.find_element(By.XPATH,'//*[@id="lists"]/li[1]/div/img').click()
        sleep(2)
        self.browser.find_element(By.XPATH,'//*[@id="used"]').click()
        sleep(5)
        
    def test_shop_sj(self): 
       self.browser.switch_to.default_content()
       #点击商品
       self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a').click()
       sleep(2)
       #定位子框架
       iframe1 = self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
       self.browser.switch_to.frame(iframe1)     
       #点击仓库商品
       self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/ul/li[2]').click() 
       sleep(2) 
       
       # 获取列表
       shop_list = self.browser.find_elements(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')    
       if len(shop_list) != 0:
        #    列表长度不为零则上架
           self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr/td/div/a[2]').click()
           sleep(3)
           self.browser.find_element(By.CLASS_NAME,'layui-layer-btn0').click()
           sleep(3)
    
    
    
    def test_shop_edit(self):
        #商品编辑
        self.browser.switch_to.default_content()
        #点击商品按钮
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        sleep(2)
        #定位子框架
        iframe1 = self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)   
       #  编辑
        self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr[1]/td/div/a[1]').click() 
        sleep(3)
       #进入编辑商品子框架    
        shop_edit_iframe = self.browser.find_element(By.XPATH,'/html/body/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(shop_edit_iframe)
       # 清空文本框数据
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').clear()
        sleep(3)
        # 再次输入商品名称数据        
        now = datetime.datetime.now().strftime('    %d/%m/%Y')
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').send_keys('各种笔类' +       now)
       
    #    重新选择商品大类
        # 再进入商品编辑子框架
        self.browser.switch_to.default_content()
        self.browser.switch_to.frame(iframe1)
        self.browser.switch_to.frame(shop_edit_iframe)
        sleep(3)
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(1)
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[3]').click()
        sleep(1) 
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(1)
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
        sleep(1)
       
        # 再进入商品编辑子框架
        self.browser.switch_to.default_content()
        self.browser.switch_to.frame(iframe1)
        self.browser.switch_to.frame(shop_edit_iframe)
        sleep(3)
        # 点击价格库存
        self.browser.find_element(By.XPATH,'/html/body/div/ul/li[2]').click()
        sleep(2)
        # 定位并清除价格、成本价、库存输入框数据
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').clear()
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').clear()
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').clear()
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').clear()
        sleep(3)
        # 再次定位价格、成本价、库存输入框并输入数据
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').send_keys(10)
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').send_keys(20)
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').send_keys(30)
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').send_keys(150)
        sleep(1)    
          # 修改后保存 
        self.browser.switch_to.default_content()
        self.browser.switch_to.frame(iframe1)
        self.browser.find_element(By.LINK_TEXT,'保存').click()

    
    def test_logout(self):
        self.browser.switch_to.default_content()
        # 鼠标悬停
        dt = self.browser.find_element(By.XPATH,'//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/a/cite')
        ActionChains(self.browser).move_to_element(dt).perform()
        sleep(3)
        self.browser.find_element(By.XPATH,'//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a').click()               
        
        
        
        
if __name__ == '__main__':
    likeShopTest=LikeShopTest()
    likeShopTest.test_login()
    likeShopTest.test_shop_list()
    # likeShopTest.test_shop_add()
    likeShopTest.test_shop_sj()
    likeShopTest.test_shop_edit()
    likeShopTest.test_logout()
    